package com.eastfair.projectcore.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.annotation.annotation.base.BaseControllerInfo;
import com.eastfair.auth.dto.UserAccountDto;
import com.eastfair.auth.entity.MenuRole;
import com.eastfair.auth.vo.UserAccountVo;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.constant.DictionaryTypeContext;
import com.eastfair.core.context.ContextConstants;
import com.eastfair.projectcore.dto.MenuDTO;
import com.eastfair.projectcore.entity.Dictionary;
import com.eastfair.projectcore.dto.DictionarySaveDTO;
import com.eastfair.projectcore.dto.DictionaryUpdateDTO;
import com.eastfair.projectcore.dto.DictionaryPageQuery;
import com.eastfair.projectcore.entity.DictionaryType;
import com.eastfair.projectcore.enumeration.MenuAffiliationEnum;
import com.eastfair.projectcore.service.DictionaryService;

import java.io.Serializable;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.projectcore.service.DictionaryTypeService;
import com.netflix.loadbalancer.Server;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.eastfair.boot.request.PageParams;

import javax.validation.Valid;


/**
 * <p>
 * 前端控制器
 * 字典项
 * </p>
 *
 * @author ligang
 * @date 2021-11-01
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/dictionary")
@Api(value = "Dictionary", tags = "字典项")
public class DictionaryController extends SuperController<DictionaryService, Long, Dictionary, DictionaryPageQuery, DictionarySaveDTO, DictionaryUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Autowired
    DictionaryTypeService dictionaryTypeService;


    @Override
    public void handlerResult(IPage<Dictionary> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<Dictionary> dictionaryList = list.stream().map((map) -> {
            Dictionary dictionary = Dictionary.builder().build();
            //TODO 请在这里完成转换
            return dictionary;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(dictionaryList));
    }


    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<DictionaryPageQuery> params) {
        DictionaryPageQuery pageQuery = params.getModel();
        //分页处理
    }


    /**
     * 查询字典项
     *
     * @param type 菜单id
     * @return 查询结果
     */
    @ApiOperation(value = "查询字典项类型", notes = "查询字典项类型")
    @GetMapping("/queryDict")
    public R<List<Dictionary>> queryDict(String type) {
        List<Dictionary> dictionaryList = baseService.queryDict(type);
        log.info("dictionaryList is {}", JSONObject.toJSONString(dictionaryList));
        return success(dictionaryList);
    }

    /**
     * 查询字典项
     *
     * @return 查询结果
     */
    @ApiOperation(value = "查询字典项类型", notes = "查询字典项类型")
    @PostMapping("/list2")
    public R<List<Dictionary>> list2(@RequestBody Dictionary dictionary) {
        List<Dictionary> dictionaryList = baseService.list2(dictionary, null);
        return success(dictionaryList);
    }

    /**
     * 查询字典项
     *
     * @return 查询结果
     */
    @ApiOperation(value = "查询字典项类型", notes = "查询字典项类型")
    @PostMapping("/list3")
    public R<List<Dictionary>> list3(@RequestBody Dictionary dictionary) {
        List<Dictionary> dictionaryList = baseService.list3(dictionary, null);
        return success(dictionaryList);
    }

    /**
     * 分组查询类型
     *
     * @return 查询结果
     */
    @ApiOperation(value = "分组查询类型", notes = "分组查询类型")
    @GetMapping("/queryDictGroupType")
    public R<List<Dictionary>> queryDictGroupType() {
        return success(baseService.queryDictGroupType());
    }

    @GetMapping("/findByIds")
    public Map<Serializable, Object> findUserByIds(@RequestParam(value = "ids") Set<Serializable> ids) {

        return baseService.findByIds(ids);
    }

    /**
     * 初始化数据
     *
     * @return
     */
    @GetMapping("/initData")
    public R initDictData() {
//        baseService.initDictData();
        //重新加载数据
//        Dictionary dictionary = new Dictionary();
//        List<Dictionary> dictionaryList = baseService.list(dictionary,null);
//        Map<String, List<Dictionary>> groupList = dictionaryList.stream().collect(Collectors.groupingBy(Dictionary::getType));
//
//        for(List<Dictionary> dictionaries : groupList.values()){
//            Dictionary dictionary1 = dictionaries.get(0);
//            DictionaryType dictionaryType= new DictionaryType();
//            dictionaryType.setType(dictionary1.getType());
//            dictionaryType.setLabel(dictionary1.getLabel());
//            dictionaryType.setIsDeleted(BusinessConstant.NO);
//            dictionaryType.setIsEnabled(BusinessConstant.YES);
//            dictionaryTypeService.save(dictionaryType);
//            for(Dictionary dictionary2 : dictionaries){
//                dictionary2.setTypeId(dictionaryType.getId());
//                baseService.updateById(dictionary2);
//            }
//        }

        return R.success();
    }

    /**
     * 初始化数据
     *
     * @return
     */
    @PostMapping("/findIds")
    public R findIds(@RequestBody List<Long> ids) {
        return R.success(baseService.listByIds(ids));
    }

    /**
     * 初始化数据
     *
     * @return
     */
    @PostMapping("/listByObject")
    public R listByObject(@RequestBody DictionaryPageQuery dictionaryPageQuery) {
        return R.success(baseService.listByObject(dictionaryPageQuery));
    }

    /**
     * 初始化数据
     *
     * @return
     */
    @PostMapping("/findIds2")
    public R findIds2(@RequestBody List<Long> ids) {
        DictionaryPageQuery dictionaryPageQuery = new DictionaryPageQuery();
        dictionaryPageQuery.setCode("2222");
        return R.success(baseService.listByIds2(ids, dictionaryPageQuery));
    }


    /**
     * 执行查询
     * <p>
     * 可以覆盖后重写查询逻辑
     *
     * @param params 分页参数
     * @return
     */
    @ApiOperation(value = "分页列表查询")
    @PostMapping(value = "/pageVo")
    public R<IPage<Dictionary>> pageVo(@RequestBody @Valid PageParams<Dictionary> params) {
        // 处理参数
        return baseService.pageVo(params);
    }


    /**
     * 重新加载枚举类字典
     *
     * @return
     */
    @GetMapping("/loadEnumData")
    public R loadEnumData() {
        baseService.delType(DictionaryTypeContext.MENU_AFFILIATION);
        Dictionary dictionary = new Dictionary();
        for (MenuAffiliationEnum menuAffiliationEnum : MenuAffiliationEnum.values()) {
            dictionary.setName(menuAffiliationEnum.getDesc());
            dictionary.setType(DictionaryTypeContext.MENU_AFFILIATION);
            dictionary.setLabel("归属系统");
            dictionary.setIsEnabled(BusinessConstant.YES);
            dictionary.setCode(menuAffiliationEnum.getCode());
            baseService.save(dictionary);
        }
        return R.success();
    }

    /**
     * 删除组
     * <p>
     * 可以覆盖后重写查询逻辑
     *
     * @return
     */
    @ApiOperation(value = "删除组")
    @PostMapping(value = "/removeType")
    public R removeType(@RequestBody Dictionary dictionary) {
        Dictionary query = new Dictionary();
        query.setType(dictionary.getType());
        List<Dictionary> dictionaryList = baseService.list(query, null);
        if (dictionaryList != null && !dictionaryList.isEmpty()) {
            for (Dictionary dictionary1 : dictionaryList) {
                dictionary1.setIsDeleted(BusinessConstant.YES);
                baseService.updateById(dictionary1);
            }
        }
        return R.success();
    }

}
